using System;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace PassThePuzzle
{
    public static class Program
    {
        public static SettingsDlg settingsDlg;
        public static Gameform gameform;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            FMOD.System fModSystem = null;
            FMOD.Factory.System_Create(ref fModSystem);
            uint version = 0;
            fModSystem.getVersion(ref version);
            if (version < FMOD.VERSION.number)
            {
                MessageBox.Show("Error! You are using an old version of FMOD ("
                  + version.ToString("X") + ").  This program requires "
                  + FMOD.VERSION.number.ToString("X") + ".");
                return;
            }
            fModSystem.init(32, FMOD.INITFLAG.NORMAL, (IntPtr) null);

            gameform = new Gameform(fModSystem);
            Application.Run(settingsDlg = new SettingsDlg());
        }
    }

    [XmlRoot("settings")]
    public class Settings
    {
        public string letters;
        public int roundTimeMin;
        public int roundTimeMax;
        public int puzzleSizeMin;
        public int puzzleSizeMax;
        public int solutionCountMin;
        public int solutionCountMax;
        public bool showSolutionCount;
        public bool lettersBeginWith;
        public bool lettersEndWith;
        public bool lettersGivenOrder;
        public bool lettersConsecutive;
        public bool keepScores;
        public List<score> scores;
        //[XmlElement("categoryFile")]
        public string[] categoryFiles;


        public score AddScores(string newName)
        {
            if (scores == null)
                scores = new List<score>();
            var s = new score { name = newName, value = 0 };
            scores.Add(s);
            scores.Sort();
            return s;
        }

        public void RemoveScores(score scoreToRemove)
        {
            scores.Remove(scoreToRemove);
        }

        public void RemoveAllScores()
        {
            scores = new List<score>();
        }

    }

    public class score : IComparable<score>
    {
        public event EventHandler SomethingChanged;
        private string _name;
        private float _value;

        [XmlAttribute("name")]
        public string name
        {
            get { return _name; }
            set
            {
                if (value != _name)
                {
                    _name = value;
                    if (SomethingChanged != null)
                        SomethingChanged(this, new EventArgs());
                }
            }
        }
        [XmlAttribute("value")]
        public float value
        {
            get { return _value; }
            set
            {
                if (value != _value)
                {
                    _value = value;
                    if (SomethingChanged != null)
                        SomethingChanged(this, new EventArgs());
                }
            }
        }

        public int CompareTo(score other)
        {
            return name.CompareTo(other.name);
        }
    }

    public class scoreListItem : ListViewItem
    {
        public score Score;
        public scoreListItem(score score)
            : base(new string[] { score.name, score.value.ToString() })
        {
            Score = score;
            Score.SomethingChanged += new EventHandler(somethingChanged);
        }

        void somethingChanged(object sender, EventArgs e)
        {
            base.SubItems[0].Text = Score.name;
            base.SubItems[1].Text = Score.value.ToString();
        }
    }
}
